import { ajax, XHR } from '@/common/request'
import { IM_API_URL } from '@/common/env'
import Storage from '@/common/Storage'

export const Fetch = function ({ url, param = {}, options = false, method = 'post' }) {
  try {
    const data = XHR.formData(param)

    return ajax({
      url: IM_API_URL + url,
      method,
      data,
      options,
      headerExt: { imtoken: Storage.get('IM_ACCESS_TOKEN') || '' },
      isAddHost: false // 不需要加host
    })
  } catch (e) {
    console.log('request error :' + JSON.stringify(e))
  }
}

// 生成、查询token
export const getAccessToken = (param, options) => Fetch({ url: '/api/im/getAccessToken', param, options })

// 绑定用户
export const bindUid = (param, options) => Fetch({ url: '/api/im/bind', param, options })

// 绑定用户
export const unBindUid = (param, options) => Fetch({ url: '/api/im/unbind', param, options })

// 绑定用户
export const getGroupCount = (param, options) => Fetch({ url: '/api/im/getGroupCount', param, options })

// 发送消息
export const sendMsg = (param, options) => Fetch({ url: '/api/im/sendMsg', param, options })

// 获取更多消息
export const getMsgList = (param, options) => Fetch({ url: '/api/im/getMsgList', param, options })

// 查询会话列表
export const getChatList = (param, options) => Fetch({ url: '/api/im/getChatList', param, options })
